package Ep01_Array.Mar03rd_0209;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/3/3 9:13
 * 说明：在数组nums[]中寻找sum大于target的子序列。     滑动窗口
 */

public class Mine {

    public static void main(String[] args) {
        int[] nums = {1,1,1,1,1,1,1,1};
        Mine e = new Mine();
        System.out.println(e.minSubArrayLen(11, nums));
    }

    public int minSubArrayLen(int target, int[] nums){
        int result = Integer.MAX_VALUE;
        int start = 0;
        int sum = 0;

        for (int end = 0; end < nums.length; end++) {
            sum += nums[end];
            while (sum >= target){
                result = Math.min(result, end-start+1);
                sum -= nums[start++];
            }
        }
        // 判断result是否没有进行更改，若是，则说明找不到子数组，返回0。否则返回最小的子数组长度
        return result == Integer.MAX_VALUE ? 0:result;
    }
}
